//
//  GQHRouteNewView.m
//  Xanthium
//
//  Created by GuanQinghao on 16/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHRouteNewView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"

@interface GQHRouteNewView () <UITableViewDelegate,UITableViewDataSource> {
    
    NSArray *titleArray;
}

@end

@implementation GQHRouteNewView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    titleArray = @[@"起始地",@"目的地"];
    if (!_qh_contentArray) {
        
        _qh_contentArray = [NSMutableArray arrayWithArray:@[@"请选择",@"请选择"]];
    }
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    GQHCustomTextDoubleHorizontalCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        
        cell = [[GQHCustomTextDoubleHorizontalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.qh_leftLabel.text = titleArray[indexPath.row];
    cell.qh_rightLabel.text = _qh_contentArray[indexPath.row];
    
    /// 重新布局
    [cell.qh_leftLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(cell.contentView.mas_centerX).with.inset(50);
        
    }];
    [cell.qh_rightLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(cell.contentView.mas_centerX).with.inset(-50);
    }];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

@end
